package logic

import (
	"context"
	"errors"
	"video/model/mysql"

	"video/videorpc/internal/svc"
	"video/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectAddLogic {
	return &CollectAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 收藏功能
func (l *CollectAddLogic) CollectAdd(in *videorpc.CollectAddRequest) (*videorpc.CollectAddResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	err := user.FindUserById(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("用户不存在")
	}

	var video mysql.Video

	err = video.FindVideoById(mysql.DB, in.VideoId)
	if err != nil {
		return nil, errors.New("视频不存在")
	}

	if in.Status == 0 {
		return nil, errors.New("已收藏该视频")
	}

	collect := &mysql.Collection{
		UserId:  int(in.UserId),
		VideoId: int(in.VideoId),
		Status:  int(in.Status),
	}

	err = collect.CollectAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("收藏失败")
	}

	return &videorpc.CollectAddResponse{
		CollectionId: int64(collect.ID),
	}, nil
}
